package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.ImgItemsBean;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 添加图片的适配器
 *
 * @author GuiyouZhang
 *         Date：2015年11月26日 上午9:26:31
 *         Email:guiyouzhang@163.com
 */
public class AddImageAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<ImgItemsBean> images;//添加本地图片的地址
    private OnImageClickListener clickListener;
    private boolean showDelete = true;//是否显示删除图标
    private boolean showAdd = true;//是否显示添加图片的图标
    private int maxCount = 4;

    public AddImageAdapter(Context context, ArrayList<ImgItemsBean> images) {
        this.mContext = context;
        this.images = images;
    }

    public void setImageClickListener(OnImageClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setShowAdd(boolean showAdd) {
        this.showAdd = showAdd;
    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    public void setShowDelete(boolean showDelete) {
        this.showDelete = showDelete;
    }

    @Override
    public int getCount() {
        int count = images.size();
        if (showAdd)
            count++;
        return count > maxCount ? maxCount : count;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_add_image, null);
        ViewHolder holder = new ViewHolder(convertView);
        convertView.setTag(holder);
        setDatas(holder, position);
        return convertView;
    }

    private void setDatas(ViewHolder holder, final int position) {
        if (showAdd && position == images.size() && images.size() < maxCount) {
            holder.ivContent.setScaleType(ScaleType.CENTER_INSIDE);
            holder.ivContent.setImageResource(R.drawable.plus_orange);
            holder.ivDelete.setVisibility(View.GONE);
        } else {
            holder.ivContent.setScaleType(ScaleType.CENTER_CROP);
            String path = images.get(position).getImgurl();
            if (path.startsWith("http"))
                ImageUtils.loadSmalImage(mContext, path, holder.ivContent);
            else
                ImageUtils.loadLocalImage(mContext, path, holder.ivContent);
            if (showDelete) {
                holder.ivDelete.setVisibility(View.VISIBLE);
            }else
                holder.ivDelete.setVisibility(View.GONE);
        }
        holder.ivDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clickListener != null) {
                    clickListener.onDeleteClickListener(position,  images.get(position));
                }
            }
        });
        holder.ivContent.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clickListener != null) {
                    clickListener.onImageClickListener(position);
                }
            }
        });
    }

    static class ViewHolder {
        @Bind(R.id.iv_content)
        ImageView ivContent;
        @Bind(R.id.iv_delete)
        ImageView ivDelete;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface OnImageClickListener {
        public void onImageClickListener(int position);
        public void onDeleteClickListener(int position, ImgItemsBean item);
    }
}
